package com.jingsong.studentapp.page;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.jingsong.studentapp.R;
import com.jingsong.studentapp.adapter.LessonAdapter;
import com.jingsong.studentapp.fragment.LessionItemFragment;
import com.jingsong.studentapp.view.NoSlidingViewPager;

import java.util.ArrayList;
import java.util.List;

public class LessonInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private NoSlidingViewPager viewPager;
    private TextView tvTab1;
    private TextView tvTab2;
    private TextView tvIden1;
    private TextView tvIden2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson_info);
        findViewById(R.id.ivBack).setOnClickListener(this);

        TextView tvTitle = findViewById(R.id.tvName);
        tvTitle.setText("课时名称1");

        viewPager = findViewById(R.id.vpPage);

        findViewById(R.id.llTab1).setOnClickListener(this);
        findViewById(R.id.llTab2).setOnClickListener(this);

        tvTab1 = findViewById(R.id.tab1);
        tvTab2 = findViewById(R.id.tab2);

        tvIden1 = findViewById(R.id.tvIden1);
        tvIden2 = findViewById(R.id.tvIden2);

        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            fragments.add(LessionItemFragment.newInstance(i));
        }

        LessonAdapter adapter = new LessonAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);

        showTab(0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.llTab1:
                viewPager.setCurrentItem(0);
                showTab(0);
                break;
            case R.id.llTab2:
                viewPager.setCurrentItem(1);
                showTab(1);
                break;
        }
    }

    private void showTab(int index) {
        switch (index) {
            case 0:
                tvTab1.setTextColor(Color.BLACK);
                tvTab2.setTextColor(getResources().getColor(R.color.item_text));

                tvIden1.setVisibility(View.VISIBLE);
                tvIden2.setVisibility(View.INVISIBLE);
                break;
            case 1:
                tvTab2.setTextColor(Color.BLACK);
                tvTab1.setTextColor(getResources().getColor(R.color.item_text));

                tvIden2.setVisibility(View.VISIBLE);
                tvIden1.setVisibility(View.INVISIBLE);
                break;
        }
    }
}